/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: MissionManager.java 643 2010-07-30 05:56:41Z Victor $
 *   $LastChangedDate: 2010-07-30 05:56:41 +0000 (Fri, 30 Jul 2010) $
 *   $LastChangedBy: Victor $
 *--------------------------------------------------------------------------
 */
package battlefield.manager;

import battlefield.BattlefieldException;
import battlefield.entity.Mission;
import battlefield.entity.Player;
import battlefield.util.DataUtils;
import java.util.HashMap;
import java.util.Stack;

/**
 *
 * @author xiankun
 */
public class MissionManager {

//    private Stack<String> selectMission;

    /**
     * Constructs a MissionManager object
     */
    public MissionManager() {
//        selectMission = new Stack<String>();
    }

    private HashMap<String, Mission> loadMissions() throws BattlefieldException {
        HashMap<String, Mission> missions = new HashMap<String, Mission>();
        String[] allData = DataUtils.readLines(this.getClass().getResourceAsStream("/battlefield/missions.txt"));
        String delimiter = ";";
        String[] temp;

        for (int i = 0; i < allData.length; i++) {
            String str = allData[i];
            temp = str.split(delimiter);
            Mission mission = new Mission();
            mission.setMissionName(temp[0]);
            mission.setGoldReceive(Integer.parseInt(temp[1]));
            mission.setImageSrc(temp[2]);
            mission.setEnable(Boolean.parseBoolean(temp[3]));
            mission.setMissionLevel(Integer.parseInt(temp[4]));

            //populateSelectMissionToEnable(temp[0], Boolean.parseBoolean(temp[3]), Integer.parseInt(temp[4]));
            missions.put(temp[0], mission);
        }
        return missions;
    }

    /**
     * Enables mission that players are allowed to do
     * @param missionName
     * @param enable
     * @param missionLevel
     */
    public Stack<String> populateSelectMissionToEnable() {
        Stack<String> selectMission = new Stack<String>();

        String[] allData = DataUtils.readLines(this.getClass().getResourceAsStream("/battlefield/missions.txt"));
        String delimiter = ";";
        String[] temp;
        for (int i = 0; i < allData.length; i++)
        {
            String str = allData[i];
            temp = str.split(delimiter);
            boolean enable = Boolean.parseBoolean(temp[3]);
            if (enable == false) {

                selectMission.add(Integer.parseInt(temp[4]) - 1, temp[0]);
            }
        }
        //rearranging the stack, put the string object in sequence of level 1 to n
        Stack<String> tempStack = new Stack<String>();

        for(int i=0;i<allData.length-1;i++)
        {
            tempStack.add(i, selectMission.pop());
        }


        return tempStack;
        
    }

    private String selectMissionToEnable(Stack<String> selectMission) {
        if (selectMission.size() > 0) {
            return selectMission.pop();
        } else {
            return "";
        }
    }

    /**
     * Creates a hash map of all missions that a player is allowed to do
     * @param player
     */
    public void createMissions(Player player) throws BattlefieldException {
        try
        {
            if(player==null)
            {
                throw new BattlefieldException("Player is null");
            }
            player.setMissions(loadMissions());
            player.setMissionStack(populateSelectMissionToEnable());
        }
        catch(Exception ex)
        {
            new BattlefieldException(ex.getMessage());
        }
        
    }

    /**
     * Update the progress of a mission under a player and return an amount of 
     * gold as reward for the player
     * @param missionName
     * @param player
     * @return amount of gold
     */
    public int doMission(String missionName, Player player) throws BattlefieldException {
        if(player==null)
        {
            throw new BattlefieldException("Player is null");
        }
        else if(missionName.trim().equals(""))
        {
            throw new BattlefieldException("mission name is invalid");
        }
        Mission mission = player.getMissions().get(missionName);
        int returnGold = mission.getGoldReceive();
        updateMissionProgress(mission);
        updateComplete(player.getMissions(), mission, player.getMissionStack());
        return returnGold;
    }

    private void updateMissionProgress(Mission mission) {
        int newProgress = mission.getMissionComplete() + 10;
        mission.setMissionComplete(newProgress);
    }

    private void updateComplete(HashMap<String, Mission> missions, Mission mission, Stack<String> selectMIssion) {
        if (mission.getMissionComplete() >= 100) {
            mission.setEnable(false);

            String newEnableMission = selectMissionToEnable(selectMIssion);
            if (newEnableMission.trim().equals("") == false) {
                Mission enableMission = missions.get(newEnableMission);
                enableMission.setEnable(true);
            }

        }

    }
}
